//
//  NSString+Additions.m
//  GomeMarketingClient
//
//  Created by Kelly xu on 2017/1/6.
//  Copyright © 2017年 GomeFinance. All rights reserved.
//

#import "NSString+Additions.h"
//如果只是使用random函数，那么只要做如下导入即可
#import <CommonCrypto/CommonRandom.h>
@implementation NSString (Additions)
-(id)JSONObject{
    NSData *jsonData = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:jsonData
                                           options:NSJSONReadingMutableContainers
                                             error:nil];
}

- (NSString*)year{
    return [[self componentsSeparatedByString:@"-"] objectAtIndex:0];
}

- (NSString*)month{
    return [[self componentsSeparatedByString:@"-"] objectAtIndex:1];
}

- (NSString*)day{
    return [[self componentsSeparatedByString:@"-"] objectAtIndex:2];
}

- (NSString *)stringByTrim {
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    return [self stringByTrimmingCharactersInSet:set];
}


- (NSString *)conversionStringWithSpaceFromString:(NSString *)string
                                        frequency:(NSInteger)frequency
                                     prefixString:(NSString *)prefixString
{
    NSInteger lopCount = string.length / frequency;
    NSMutableString *mString = [NSMutableString stringWithString:string];
    for (int i = 0; i < lopCount; i++) {
        [mString insertString:@" " atIndex:(frequency * (i + 1) + i)];
    }
    return [NSString stringWithFormat:@"%@%@", prefixString, mString];
}

//把字符串替换成星号
-(NSString *)replaceStringWithAsterisk:(NSString *)originalStr startLocation:(NSInteger)startLocation length:(NSInteger)length
{
    NSString *newStr = originalStr;
    for (int i = 0; i < length; i++) {
        NSRange range = NSMakeRange(startLocation, 1);
        newStr = [newStr stringByReplacingCharactersInRange:range withString:@"*"];
        startLocation ++;
    }
    return newStr;
}
//过滤表情符号
+ (BOOL)isContainsTwoEmoji:(NSString *)string
{
    __block BOOL returnValue = NO;
    
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length])
                               options:NSStringEnumerationByComposedCharacterSequences
                            usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
                                const unichar high = [substring characterAtIndex: 0];
                                
                                // Surrogate pair (U+1D000-1F9FF)
                                if (0xD800 <= high && high <= 0xDBFF) {
                                    const unichar low = [substring characterAtIndex: 1];
                                    const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
                                    
                                    if (0x1D000 <= codepoint && codepoint <= 0x1F9FF){
                                        returnValue = YES;
                                    }
                                    
                                    // Not surrogate pair (U+2100-27BF)
                                } else {
                                    if (0x2100 <= high && high <= 0x27BF){
                                        returnValue = YES;
                                    }
                                }
                            }];
    
    return returnValue;
//    __block BOOL isEomji = NO;
//    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
//     ^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
//         const unichar hs = [substring characterAtIndex:0];
//         //         NSLog(@"hs++++++++%04x",hs);
//         if (0xd800 <= hs && hs <= 0xdbff) {
//             if (substring.length > 1) {
//                 const unichar ls = [substring characterAtIndex:1];
//                 const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
//                 if (0x1d000 <= uc && uc <= 0x1f77f)
//                 {
//                     isEomji = YES;
//                 }
//                 //                 NSLog(@"uc++++++++%04x",uc);
//             }
//         } else if (substring.length > 1) {
//             const unichar ls = [substring characterAtIndex:1];
//             if (ls == 0x20e3|| ls ==0xfe0f) {
//                 isEomji = YES;
//             }
//             //             NSLog(@"ls++++++++%04x",ls);
//         } else {
//             if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
//                 isEomji = YES;
//             } else if (0x2B05 <= hs && hs <= 0x2b07) {
//                 isEomji = YES;
//             } else if (0x2934 <= hs && hs <= 0x2935) {
//                 isEomji = YES;
//             } else if (0x3297 <= hs && hs <= 0x3299) {
//                 isEomji = YES;
//             } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50|| hs == 0x231a ) {
//                 isEomji = YES;
//             }
//         }
//         
//     }];
//    return isEomji;
}


- (BOOL)isFloatValue {
    if ([self containsString:@"+"] || [self containsString:@"-"]) {
        return NO;
    }
    NSScanner* scan = [NSScanner scannerWithString:self];
    float val;
    return  [scan scanFloat:&val] && [scan isAtEnd];
}


- (BOOL)isIntValue {
    if ([self containsString:@"+"] || [self containsString:@"-"]) {
        return NO;
    }
    NSScanner* scan = [NSScanner scannerWithString:self];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}

- (BOOL)valitateMoney
{
    if ([self containsString:@"."]) {
        if (self.length > 13) {
            return NO;
        }
    } else {
        if (self.length > 10) {
            return NO;
        }
    }
    
    return YES;
}

- (int)charLength
{
    int strlength = 0;
    char* p = (char*)[self cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i = 0 ; i < [self lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
    }
    return strlength;
}

- (BOOL)checkTelNumber{
    
    NSString * strNum = nil;
    if ([self containsString:@"-"]) {
        //带 "-"符号
        strNum = @"^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$|(^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])\\d{8}$)";
    } else {
        return NO;
    }
    
    NSPredicate *checktest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strNum];
    
    return [checktest evaluateWithObject:self];
    
}
- (NSString *)randomString:(NSInteger)length{
    length = length/2;
    unsigned char digest[length];
    CCRNGStatus status = CCRandomGenerateBytes(digest, length);
    NSString *s = nil;
    if (status == kCCSuccess) {
        s = [self stringFrom:digest length:length];
    } else {
        s = self;
    }
    NSLog(@"randomLength---:%@",s);
    return s;
}
//将bytes转为字符串
- (NSString *)stringFrom:(unsigned char *)digest length:(NSInteger)leng {
    NSMutableString *string = [NSMutableString string];
    for (int i = 0; i < leng; i++) {
        [string appendFormat:@"%02x",digest[i]];
    }
    NSLog(@"final stringFrom:%@",string);
    return string;
}
//转拼音
- (NSString *)getPinyin {
    NSMutableString *mutableString = [NSMutableString stringWithString:self];
    // Boolean CFStringTransform(CFMutableStringRef string, CFRange *range, CFStringRef transform, Boolean reverse);
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformToLatin, false); // 汉字转成拼音(不知道为什么英文是拉丁语的意思)
    CFStringTransform((CFMutableStringRef)mutableString, NULL, kCFStringTransformStripDiacritics, false); // 去掉音调
    NSString *resultStr = [mutableString stringByReplacingOccurrencesOfString:@" " withString:@""];
    return resultStr;
}
@end
